package com.vint.mi;

/**
 */
public class Mi101 {
	private static String solution(String line) {
		int n =  Integer.parseInt(line);
		if (n == 1)
			return 2+"";
		if (n == 2)
			return 3+"";
		//....
		if ((n & (n + 1)) == 0)
			return 2+"";
		int m = (int) Math.sqrt(n);
		for (int i=3; i<= m; ++i) {
			if (testSame(n, i)){
				return i+"";
			}
		}
		for (int i=m; i>0; --i) {
			if (n % i == 0) {
				return (n / i - 1)+"";
			}
		}

		return n - 1 +"";
	}
	public static boolean testSame(int n, int k) {
		int bit = n % k;
		while (n != 0) {
			if(n % k != bit){
				return false;
			}
			n = n / k;
		}
		return true;
	}

	public static void main(String[] args){
		System.out.println(solution("22"));
		System.out.println(solution("15"));
	}
}
